package aa;

import java.util.*;
import java.lang.*;
import java.net.InetSocketAddress;
import java.nio.*;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

public class touc {

	public static void main(String[] args) throws Exception
	{
			String serverip = "154.221.19.38";
			int serverport = 19022;
			String key = "bigSalt";
			int localPort = 9997;
			int portNum=100;
			int increaseDose=3;
			int deDose=1;
			int pushA=2000;
			int sizeLi=1400;
			int limit = 50;
			int speed = 250;
		work( serverip,serverport, key, localPort,
				portNum, increaseDose, deDose, pushA, sizeLi, limit, speed,new logClass());
	}
	
	public static void work(String serverip,int serverport,String key2, int localPort,
			int portNum,int increaseDose,int deDose,int pushA,int sizeLi,int limit,int speed,logClass lo) throws Exception
	{
		try {
		String id = helpFunc.bytesToHex(helpFunc.getuuid_byte());
		byte[] gSalt = new byte[6];
		System.arraycopy(id.getBytes(), 0, gSalt, 0, 6);

		while (true) {
			DatagramChannel socket = DatagramChannel.open();
			structWrapper re = new structWrapper(1400);
			re.writeArr(id.getBytes());
			re.writeDouble(helpFunc.con_minRate);
			re.writeWord((short)pushA);
			re.writeWord((short) sizeLi);
			re.writeArr(gSalt);
			re.writeWord((short) portNum);
			helpFunc.makePack(re.getWriteData(), key2.getBytes());
			InetSocketAddress add = new InetSocketAddress(serverip, serverport);
			ByteBuffer temp = ByteBuffer.wrap(helpFunc.con);
			socket.send(temp, add);

			Selector selector = Selector.open();
			socket.configureBlocking(false);
			socket.register(selector, SelectionKey.OP_READ);
			int ret = selector.select(1000);
			Iterator selectedKeys = selector.selectedKeys().iterator();
			boolean find = false;
			while (selectedKeys.hasNext()) {
				SelectionKey key = (SelectionKey) selectedKeys.next();
				selectedKeys.remove();
				if (!key.isValid()) {
					continue;
				}
				if (key.channel() == socket && key.isReadable())
					find = true;
			}
			
			if (!find)
			{socket.close();
			selector.close();
				continue;
			}
			ByteBuffer dd = ByteBuffer.allocate(helpFunc.recLen);
			socket.receive(dd);
			socket.close();
			selector.close();
			byte[] j = new byte[dd.position()];
			System.arraycopy(dd.array(), 0, j, 0, dd.position());
			helpFunc.checkPackValid2(j, key2.getBytes());

			byte[] ub = helpFunc.uuid;

			if (!Arrays.equals(helpFunc.uuid, ub))
				continue;
			structWrapper st = new structWrapper(helpFunc.con);
			ArrayList<Integer> ports = new ArrayList<Integer>();
			String ss = "";
			for (int i = 0; i <portNum; i++) {
				int port = st.readWord() + 65536 / 2;
				ports.add(port);
				ss += port + ",";
			}
			helpFunc.print("server starts:" + ss);

			new connc(serverip, localPort, increaseDose, deDose, pushA, 
					sizeLi, limit, speed, ports, gSalt, lo);

		}
		
		}
		catch (Exception e) {
			helpFunc.print(e.toString());
	}
}
}
